package com.yugong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yugong.entity.dto.*;
import com.yugong.entity.vo.request.CourseScoreRequestVO;
import com.yugong.entity.vo.response.CourseScoreListResponseVO;
import com.yugong.mapper.*;
import com.yugong.service.CourseScoreService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 课程成绩业务逻辑实现类
 *
 * @author yugong
 * @date 2024/2/18
 */
@Service
public class CourseScoreServiceImpl extends ServiceImpl<CourseScoreMapper, CourseScore> implements CourseScoreService {
    @Resource
    private CourseScoreMapper courseScoreMapper;
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private OrderClassMapper orderClassMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private CourseArrangeMapper courseArrangeMapper;

    /**
     * 学生查看个人成绩
     *
     * @param studentNo 学号
     * @param pageNum   当前页
     * @param pageSize  当前页数据总数
     * @return 个人成绩
     */
    @Override
    public PageInfo<CourseScoreListResponseVO> findScoreStudent(String studentNo, Integer pageNum, Integer pageSize, String courseName) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<CourseScore> courseScoreLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseScoreLambdaQueryWrapper
                .eq(CourseScore::getCourseScoreStudentNo, studentNo)
                .orderByDesc(CourseScore::getCourseScoreCreateTime);
        List<CourseScore> courseScoreList = courseScoreMapper.selectList(courseScoreLambdaQueryWrapper);
        QueryWrapper<CourseScore> courseScoreQueryWrapper = new QueryWrapper<>();
        courseScoreQueryWrapper.eq("course_score_student_no", studentNo);
        Long courseScoreCountNumber = courseScoreMapper.selectCount(courseScoreQueryWrapper);
        List<CourseScoreListResponseVO> courseScoreListResponseVOList = new ArrayList<>();
        for (CourseScore courseScore : courseScoreList) {
            CourseScoreListResponseVO courseScoreListResponseVO = new CourseScoreListResponseVO();
            courseScoreListResponseVO.setCourseScoreOrderClassNo(courseScore.getCourseScoreOrderClassNo());
            OrderClass orderClass = orderClassMapper.selectById(courseScore.getCourseScoreOrderClassNo());
            courseScoreListResponseVO.setCourseScoreOrderClassName(orderClass.getOrderClassName());
            LambdaQueryWrapper<Course> courseLambdaQueryWrapper = new LambdaQueryWrapper<>();
            courseLambdaQueryWrapper
                    .eq(Course::getCourseNo, courseScore.getCourseScoreCourseNo())
                    .like(Course::getCourseName, courseName);
            Course course = courseMapper.selectOne(courseLambdaQueryWrapper);
            if (course == null) {
                continue;
            } else {
                courseScoreListResponseVO.setCourseScoreCourseNo(courseScore.getCourseScoreCourseNo());
                courseScoreListResponseVO.setCourseScoreCourseName(course.getCourseName());
            }
            Student student = studentMapper.selectById(courseScore.getCourseScoreStudentNo());
            courseScoreListResponseVO.setCourseScoreStudentNo(courseScore.getCourseScoreStudentNo());
            courseScoreListResponseVO.setCourseScoreStudentName(student.getStudentName());
            courseScoreListResponseVO.setCourseScoreAchievement(courseScore.getCourseScoreAchievement());
            courseScoreListResponseVO.setCourseScoreCountNumber(courseScoreCountNumber);
            courseScoreListResponseVOList.add(courseScoreListResponseVO);
        }
        return PageInfo.of(courseScoreListResponseVOList);
    }

    /**
     * 教师查看自己带的课成绩
     *
     * @param teacherNo 教师工号
     * @param pageNum   当前页
     * @param pageSize  当前页个数
     * @param courseNo  课程号
     * @return 自己带的课成绩
     */
    @Override
    public PageInfo<CourseScoreListResponseVO> findScoreTeacher(String teacherNo, Integer pageNum, Integer pageSize, String courseNo) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<CourseArrange> courseArrangeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseArrangeLambdaQueryWrapper
                .eq(CourseArrange::getCourseArrangeTeacherNo, teacherNo)
                .like(CourseArrange::getCourseArrangeCourseNo, courseNo);
        List<CourseArrange> courseArrangeList = courseArrangeMapper.selectList(courseArrangeLambdaQueryWrapper);
        List<CourseScoreListResponseVO> courseScoreListResponseVOList = new ArrayList<>();
        for (CourseArrange courseArrange : courseArrangeList) {
            LambdaQueryWrapper<CourseScore> courseScoreLambdaQueryWrapper = new LambdaQueryWrapper<>();
            courseScoreLambdaQueryWrapper
                    .eq(CourseScore::getCourseScoreCourseNo, courseArrange.getCourseArrangeCourseNo())
                    .eq(CourseScore::getCourseScoreOrderClassNo, courseArrange.getCourseArrangeOrderClassNo());
            QueryWrapper<CourseScore> courseScoreQueryWrapper = new QueryWrapper<>();
            courseScoreQueryWrapper
                    .eq("course_score_course_no", courseArrange.getCourseArrangeCourseNo())
                    .eq("course_score_order_class_no", courseArrange.getCourseArrangeOrderClassNo());
            Long total = courseScoreMapper.selectCount(courseScoreQueryWrapper);
            List<CourseScore> courseScoreList = courseScoreMapper.selectList(courseScoreLambdaQueryWrapper);
            for (CourseScore courseScore : courseScoreList) {
                CourseScoreListResponseVO courseScoreListResponseVO = new CourseScoreListResponseVO();
                Course course = courseMapper.selectById(courseArrange.getCourseArrangeCourseNo());
                if (course != null) {
                    courseScoreListResponseVO.setCourseScoreCourseNo(course.getCourseNo());
                    courseScoreListResponseVO.setCourseScoreCourseName(course.getCourseName());
                }
                OrderClass orderClass = orderClassMapper.selectById(courseArrange.getCourseArrangeOrderClassNo());
                if (orderClass != null) {
                    courseScoreListResponseVO.setCourseScoreOrderClassNo(orderClass.getOrderClassNo());
                    courseScoreListResponseVO.setCourseScoreOrderClassName(orderClass.getOrderClassName());
                }
                if (courseScore.getCourseScoreAchievement() == null) {
                    courseScoreListResponseVO.setCourseScoreAchievement(null);
                } else {
                    courseScoreListResponseVO.setCourseScoreAchievement(courseScore.getCourseScoreAchievement());
                }
                Student student = studentMapper.selectById(courseScore.getCourseScoreStudentNo());
                if (student != null) {
                    courseScoreListResponseVO.setCourseScoreStudentNo(student.getStudentNo());
                    courseScoreListResponseVO.setCourseScoreStudentName(student.getStudentName());
                }
                courseScoreListResponseVO.setCourseScoreCountNumber(total);
                courseScoreListResponseVOList.add(courseScoreListResponseVO);
            }
        }
        return PageInfo.of(courseScoreListResponseVOList);
    }

    /**
     * 企业查看自己的班的成绩
     *
     * @param businessNo  企业号
     * @param pageNum     当前页
     * @param pageSize    当前页总数
     * @param studentName 姓名
     * @return 查看自己的班的成绩
     */
    @Override
    public PageInfo<CourseScoreListResponseVO> findScoreBusiness(String businessNo, Integer pageNum, Integer pageSize, String studentName) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<OrderClass> orderClassLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderClassLambdaQueryWrapper
                .eq(OrderClass::getOrderClassBusinessNo, businessNo);
        List<OrderClass> orderClassList = orderClassMapper.selectList(orderClassLambdaQueryWrapper);
        Long courseScoreCountNumber = 0L;
        List<CourseScoreListResponseVO> courseScoreListResponseVOList = new ArrayList<>();
        for (OrderClass orderClass : orderClassList) {
            LambdaQueryWrapper<CourseScore> courseScoreLambdaQueryWrapper = new LambdaQueryWrapper<>();
            courseScoreLambdaQueryWrapper
                    .eq(CourseScore::getCourseScoreOrderClassNo, orderClass.getOrderClassNo())
                    .orderByDesc(CourseScore::getCourseScoreCreateTime);
            List<CourseScore> courseScoreList = courseScoreMapper.selectList(courseScoreLambdaQueryWrapper);
            QueryWrapper<CourseScore> courseScoreQueryWrapper = new QueryWrapper<>();
            courseScoreQueryWrapper.eq("course_score_order_class_no", orderClass.getOrderClassNo());
            Long total = courseScoreMapper.selectCount(courseScoreLambdaQueryWrapper);
            courseScoreCountNumber = courseScoreCountNumber + total;
            for (CourseScore courseScore : courseScoreList) {
                CourseScoreListResponseVO courseScoreListResponseVO = new CourseScoreListResponseVO();
                courseScoreListResponseVO.setCourseScoreOrderClassNo(courseScore.getCourseScoreOrderClassNo());
                courseScoreListResponseVO.setCourseScoreOrderClassName(orderClass.getOrderClassName());
                courseScoreListResponseVO.setCourseScoreCourseNo(courseScore.getCourseScoreCourseNo());
                Course course = courseMapper.selectById(courseScore.getCourseScoreCourseNo());
                courseScoreListResponseVO.setCourseScoreCourseName(course.getCourseName());
                LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
                studentLambdaQueryWrapper
                        .eq(Student::getStudentNo, courseScore.getCourseScoreStudentNo())
                        .like(Student::getStudentName, studentName);
                courseScoreListResponseVO.setCourseScoreStudentNo(courseScore.getCourseScoreStudentNo());
                Student student = studentMapper.selectOne(studentLambdaQueryWrapper);
                if (student == null) {
                    continue;
                } else {
                    courseScoreListResponseVO.setCourseScoreStudentName(student.getStudentName());
                    courseScoreListResponseVO.setCourseScoreAchievement(courseScore.getCourseScoreAchievement());
                    courseScoreListResponseVO.setCourseScoreCountNumber(courseScoreCountNumber);
                }
                courseScoreListResponseVOList.add(courseScoreListResponseVO);
            }
        }
        return PageInfo.of(courseScoreListResponseVOList);
    }

    /**
     * 教师打分
     *
     * @param courseScoreRequestVO 课程成绩请求实体类
     * @return 数据库影响行数
     */
    @Override
    public int gradeTeacher(CourseScoreRequestVO courseScoreRequestVO) {
        LambdaUpdateWrapper<CourseScore> courseScoreLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        courseScoreLambdaUpdateWrapper
                .eq(CourseScore::getCourseScoreCourseNo, courseScoreRequestVO.getCourseScoreCourseNo())
                .eq(CourseScore::getCourseScoreStudentNo, courseScoreRequestVO.getCourseScoreStudentNo())
                .eq(CourseScore::getCourseScoreOrderClassNo, courseScoreRequestVO.getCourseScoreOrderClassNo())
                .set(CourseScore::getCourseScoreAchievement, courseScoreRequestVO.getCourseScoreAchievement())
                .set(CourseScore::getCourseScoreUpdateMan, courseScoreRequestVO.getCourseScoreUpdateMan())
                .set(CourseScore::getCourseScoreUpdateTime, new Date());
        return courseScoreMapper.update(courseScoreLambdaUpdateWrapper);
    }
}
